﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyCore.Api.Controllers;
using MyCore.Entities;
using MyCore.Repositories;

namespace MyFavWeb.Controllers
{
    public class FavController : BaseController2
    {
        public FavController(IUnitOfWork uow) : base(uow) { }

        // GET: Fav
        public ActionResult Index()
        {

            //using (var db = new ApplicationDbContext())
            //{
            //    //int bsid = Convert.ToInt32(id);
            //    var list2 = db.Languages.ToList();
            //    ViewBag.Fav = list2;
            //}


            var list = _uow.Repository<Fav>().GetAll().Take(2).ToList();
            ViewBag.Fav = list;
            return View();
        }

        // GET: Fav/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Fav/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Fav/Create
        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Create(Fav dto)
        {
            try
            {
                //this.TryUpdateModel<Fav>(model);
                //var isExisted = _uow.Repository<Fav>().Any(a => a.BsName == dto.BsName);
                //   if (isExisted)
                //   {
                //       return BadRequest("已存在");
                //   }

                //   var model = Mapper.Map<Fav>(dto);
                //   model.BsDate = DateTime.Now.ToShortDateString();
                //   _uow.Repository<Fav>().Add(model);
                //   _uow.SaveChanges();
                // return Ok();
                using (var db = new ApplicationDbContext())
                {
                    db.Fav.Add(dto);
                    db.SaveChanges();
                }

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Fav/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Fav/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: Fav/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Fav/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}